package com.jing.yao.config;

import com.jing.yao.cache.CacheHelper;
import com.jing.yao.cache.RedisKeyEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 生产序列号
 * @author Yao
 * @since 2020-07-13
 */
@RefreshScope
public class GeneratorSerialNumber {

    @Value("${service.service.pre.num:1}")
    private String preNum;

    @Value("${service.service.length:4}")
    private Integer length;

    @Autowired
    private CacheHelper cacheHelper;

    /**
     * 日期格式
     */
    private final static String DATE_PATTERN = "yyyyMMdd";

    /**
     * 主数据固定长度
     */
    private final static int MASTER_FIXED_LENGTH = 3;

    /**
     * 明细固定长度
     */
    private final static int DETAIL_FIXED_LENGTH = 4;

    /**
     * 基础数据代码生成
     * @param prefix
     * @return
     */
    public String generatorNo(String prefix) {
        StringBuilder builder = new StringBuilder();
        //redis自增
        Long increment = cacheHelper.increment(RedisKeyEnum.SERVICE_SERIAL_NUMBER.getKey() + prefix);
        builder.append(prefix);
        builder.append(preNum);
        builder.append(StringUtils.leftPad(increment + "", length, '0'));

        return builder.toString();
    }

    /**
     * 生成应收应付主数据编号
     * @param prefix
     * @return
     */
    public String getMasterSerialNumber(String prefix) {
        String dateFormat = getDateFormat();
        String key = RedisKeyEnum.SERVICE_SERIAL_NUMBER.getKey() + prefix +":" + getDateFormat();
        //当前大小
        Long increment = cacheHelper.increment(key);

        String result = increment + "";
        int length = result.length();

        if (length < MASTER_FIXED_LENGTH ) {
            result = StringUtils.leftPad(result, MASTER_FIXED_LENGTH, '0');
        }

        return prefix + dateFormat + result;
    }

    /**
     * 获取明细编号
     * @param prefix
     * @param sum
     * @return;
     */
    public String[] getDetailSerialNumber(String prefix, int sum) {
        return getSerialNumberWithFixLength(prefix, DETAIL_FIXED_LENGTH, sum);
    }

    /**
     * 明细手动添加生成新的明细编号
     * @param prefix
     * @param max
     * @return
     */
    public String getNextDetailSerialNumber(String prefix, int max) {
        String result = (max+1) + "";
        int length = result.length();

        if (length < DETAIL_FIXED_LENGTH ) {
            result = StringUtils.leftPad(result, MASTER_FIXED_LENGTH, '0');
        }

        return prefix  + result;
    }

    /**
     * 生成单号工具类
     * @param prefix 单号前缀
     * @param subFixLength 后缀的长度
        if (length > subFixLength ) {
            subLength = length;
        }
     * @param sum 单号长度
     * @return
     */
    private String[] getSerialNumberWithFixLength(String prefix,int subFixLength, int sum) {
        //传入的数量长度
        int length = (sum + "").length();

        int subLength = subFixLength;
        if (length >= subFixLength ) {
            subLength = length;
        }

        String s = "";
        String[] result = new String[sum];
        for (int i =1; i<= sum ; i++) {
            result[i-1] = prefix + StringUtils.leftPad(s + i, subLength, '0');
        }
        return result;
    }

    /**
     * 获取当天年月日字符串
     */
    private String getDateFormat() {
        LocalDateTime localDateTime = LocalDateTime.now();
        return localDateTime.format(DateTimeFormatter.ofPattern(DATE_PATTERN));
    }
}
